<?php

namespace app\common\model;

use app\common\model\BaseModel;
use fast\Random;
use think\model\concern\SoftDelete;

class Merchant extends BaseModel
{

    use SoftDelete;

    

    // 表名
    protected $name = 'merchant';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [

    ];

    public function merchantCar(){
        return $this->hasMany('merchant_car', 'merchant_id', 'id')->where('is_delete', 1);
    }

    public function merchantArea(){
        return $this->hasMany('merchant_area', 'merchant_id', 'id')->where('is_delete', 1);
    }

	//普通状态
	public function getStatusList($status = null)
	{
		$data = ['1' => __('启用'), '2' => __('禁用')];
		if ($status){
			return $data[$status];
		}else{
			return $data;
		}
	}
	//审核状态
	public function getCheckStatusList($status = null)
	{
		$data = ['1' => __('待审核'), '2' => __('已通过'), '3' => __('已拒绝')];
		if ($status){
			return $data[$status];
		}else{
			return $data;
		}
	}

	//密码加密
	public static function saltPassword($password,$salt){

		//密码加密
		$password = md5(md5($password).$salt);
		return $password;
	}

	//服务类型
	public static function serviceList($type = null,$isFront = false){
		$data = ['1' => '到店自取', '2' => '送车上门', '3' => '免押金',];
		if ($isFront){
			$newData = $data;
			$data = [];
			foreach ($newData as $key=>$val){
				$data[$key]['id'] = $key;
				$data[$key]['value'] = $val;
			}
		}
		if ($type){
			return $data[$type];
		}else{
			return $data;
		}
	}

	//店铺收入统计
	public static function income($uid){

		$allMoney = 0;
		$dayMoney = 0;
		$yestMoney = 0;
		$monthMoney = 0;
		$yearMoney = 0;
		$time = time();

		//查询店铺余额记录
		$balanceInfo = BalanceLog::field('amount,createtime')->where(['merchant_id' => $uid,'type' => 1])->select()->toArray();
		if (count($balanceInfo)){
			$dataTime = date('Y-m-d');
			//今日
			$dayS = strtotime($dataTime);
			$dayN = strtotime($dataTime . '23:59:59');
			//昨日
			$daySz = $dayS - 86400;
			$dayNz = $dayN - 86400;
			//本月
			$monthS = strtotime(date('Y-m-01', $dayS));
			$a = date('Y-m-01', strtotime(date('Y-m-d')));
			$monthN = strtotime(date('Y-m-d', strtotime("$a +1 month -1 day")));
			//年度
			$yearS = strtotime(date('Y').'-01-01');
			$yearN = strtotime(date('Y').'-12-31 23:59:59');
			foreach ($balanceInfo as $key=>$val){
				//总收入
				$allMoney = $allMoney + $val['amount'];
				//今日
				if ($time >= $dayS && $time <= $dayN){
					$dayMoney = $dayMoney + $val['amount'];
				}
				//昨日
				if ($time >= $daySz && $time <= $dayNz){
					$yestMoney = $yestMoney + $val['amount'];
				}
				//本月
				if ($time >= $monthS && $time <= $monthN){
                    $monthMoney = $monthMoney + $val['amount'];
				}
				//年度
				if ($time >= $yearS && $time <= $yearN){
					$yearMoney = $yearMoney + $val['amount'];
				}
			}
		}
		$income = [
			'all_money' => sprintf("%.2f",$allMoney),
			'day_money' => sprintf("%.2f",$dayMoney),
			'yest_money' => sprintf("%.2f",$yestMoney),
			'month_money' => sprintf("%.2f",$monthMoney),
			'year_money' => sprintf("%.2f",$yearMoney)
		];
		return $income;
	}

    

    







}
